home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / wb-tools / toolmanager / source / library / iconobj.c < prev    next >
C/C++ Source or Header  |  1994-06-06  |  11KB  |  317 lines

  1. /*
  2.  * iconobj.c  V2.1
  3.  *
  4.  * TMObject, Type: Icon
  5.  *
  6.  * (c) 1990-1993 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* extended TMObject structure for TMOBJTYPE_ICON objects */
  12. struct TMObjectIcon {
  13.                      struct TMObject     io_Object;
  14.                      ULONG               io_Flags;
  15.                      ULONG               io_LeftEdge;
  16.                      ULONG               io_TopEdge;
  17.                      struct TMLink      *io_ExecLink;
  18.                      struct TMLinkImage *io_ImageLink;
  19.                      struct TMLink      *io_SoundLink;
  20.                      struct TMLink       io_Link;
  21.                      struct DiskObject  *io_DiskObj;
  22.                      void               *io_AppIcon;
  23.                     };
  24.  
  25. /* io_Flags */
  26. #define IO_ShowName (1L<<0)
  27.  
  28. /* Tag to Flag mapping table for PackBoolTags */
  29. static struct TagItem flagmap[]={TMOP_ShowName, IO_ShowName,
  30.                                  TAG_DONE};
  31.  
  32. /* Create an AppIcon */
  33. static void CreateAppIcon(struct TMObjectIcon *tmobj, char *name)
  34. {
  35.  /* Add AppIcon to Workbench if image is supplied */
  36.  if (tmobj->io_ImageLink && tmobj->io_ImageLink->tmli_Normal) {
  37.   struct DiskObject *dobj;
  38.  
  39.   if (dobj=AllocMem(sizeof(struct DiskObject),MEMF_PUBLIC|MEMF_CLEAR)) {
  40.    /* Init DiskObject structure */
  41.    dobj->do_Version=WB_DISKVERSION;
  42.    dobj->do_Gadget.Width=tmobj->io_ImageLink->tmli_Width;
  43.    dobj->do_Gadget.Height=tmobj->io_ImageLink->tmli_Height;
  44.    dobj->do_Gadget.GadgetRender=tmobj->io_ImageLink->tmli_Normal;
  45.    if (dobj->do_Gadget.SelectRender=tmobj->io_ImageLink->tmli_Selected)
  46.     dobj->do_Gadget.Flags=GFLG_GADGHIMAGE|GFLG_GADGIMAGE;
  47.    else
  48.     dobj->do_Gadget.Flags=GFLG_GADGHCOMP|GFLG_GADGIMAGE;
  49.    dobj->do_Gadget.Activation=GACT_RELVERIFY|GACT_IMMEDIATE;
  50.    dobj->do_Gadget.GadgetType=GTYP_BOOLGADGET;
  51.    dobj->do_Gadget.UserData=(APTR) WB_DISKREVISION;
  52.    dobj->do_CurrentX=tmobj->io_LeftEdge;
  53.    dobj->do_CurrentY=tmobj->io_TopEdge;
  54.  
  55.    DEBUG_PRINTF("Normal 0x%08lx",dobj->do_Gadget.GadgetRender);
  56.    DEBUG_PRINTF(" Selected 0x%08lx",dobj->do_Gadget.SelectRender);
  57.    DEBUG_PRINTF(" Width %4ld",dobj->do_Gadget.Width);
  58.    DEBUG_PRINTF(" Height %4ld",dobj->do_Gadget.Height);
  59.    DEBUG_PRINTF(" X %4ld",dobj->do_CurrentX);
  60.    DEBUG_PRINTF(" Y %4ld\n",dobj->do_CurrentY);
  61.  
  62.    /* ARGGGGGH "SoftError:0 Too many redos (5)" hits again :-( */
  63.    if (tmobj->io_AppIcon=AddAppIconA((ULONG) &tmobj->io_Link,NULL,
  64.                             (tmobj->io_Flags & IO_ShowName) ?
  65.                             name : DefaultNoName, AppMsgPort,NULL,dobj,NULL))
  66.     tmobj->io_DiskObj=dobj;
  67.    else
  68.     FreeMem(dobj,sizeof(struct DiskObject));
  69.   }
  70.  }
  71. }
  72.  
  73. /* Create an Icon object */
  74. struct TMObject *CreateTMObjectIcon(struct TMHandle *handle, char *name,
  75.                                     struct TagItem *tags)
  76. {
  77.  /* Open Workbench */
  78.  if (GetWorkbench()) {
  79.   struct TMObjectIcon *tmobj;
  80.  
  81.   /* allocate memory for object */
  82.   if (tmobj=(struct TMObjectIcon *)
  83.              AllocateTMObject(sizeof(struct TMObjectIcon))) {
  84.    struct TagItem *ti,*tstate;
  85.  
  86.    /* Scan tag list */
  87.    tstate=tags;
  88.    while (ti=NextTagItem(&tstate)) {
  89.  
  90.     DEBUG_PRINTF("Got Tag (0x%08lx)\n",ti->ti_Tag);
  91.  
  92.     switch (ti->ti_Tag) {
  93.      case TMOP_Exec:    if (ti->ti_Data) {
  94.                          if (tmobj->io_ExecLink) /* Already got a link? */
  95.                           /* Yes, remove it! */
  96.                           RemLinkTMObject(tmobj->io_ExecLink);
  97.  
  98.                          /* Create new link to exec object */
  99.                          tmobj->io_ExecLink=AddLinkTMObject(handle,
  100.                                                             (char *)
  101.                                                              ti->ti_Data,
  102.                                                             TMOBJTYPE_EXEC,
  103.                                                             (struct TMObject *)
  104.                                                              tmobj);
  105.                         }
  106.                         break;
  107.      case TMOP_Image:   if (ti->ti_Data) {
  108.                          if (tmobj->io_ImageLink) /* Already got a link? */
  109.                           /* Yes, remove it! */
  110.                           RemLinkTMObject((struct TMLink *)
  111.                                           tmobj->io_ImageLink);
  112.  
  113.                          /* Create new link to exec object */
  114.                          tmobj->io_ImageLink=(struct TMLinkImage *)
  115.                                   AddLinkTMObject(handle, (char *) ti->ti_Data,
  116.                                                   TMOBJTYPE_IMAGE,
  117.                                                   (struct TMObject *) tmobj);
  118.                         }
  119.                         break;
  120.      case TMOP_LeftEdge:tmobj->io_LeftEdge=ti->ti_Data;
  121.                         break;
  122.      case TMOP_Sound:   if (ti->ti_Data) {
  123.                          if (tmobj->io_SoundLink) /* Already got a link? */
  124.                           RemLinkTMObject(tmobj->io_SoundLink); /* Remove it! */
  125.  
  126.                          /* Create new link to exec object */
  127.                          tmobj->io_SoundLink=AddLinkTMObject(handle,
  128.                                                              (char *)
  129.                                                               ti->ti_Data,
  130.                                                              TMOBJTYPE_SOUND,
  131.                                                              (struct TMObject *)
  132.                                                               tmobj);
  133.                         }
  134.                         break;
  135.      case TMOP_TopEdge: tmobj->io_TopEdge=ti->ti_Data;
  136.                         break;
  137.     }
  138.    }
  139.  
  140.    /* Set flags */
  141.    tmobj->io_Flags=PackBoolTags(IO_ShowName,tags,flagmap);
  142.  
  143.    /* Create AppIcon */
  144.    CreateAppIcon(tmobj,name);
  145.  
  146.    /* Initialize rest of structure */
  147.    tmobj->io_Link.tml_Linked=(struct TMObject *) tmobj;
  148.  
  149.    /* All OK */
  150.    return(tmobj);
  151.   }
  152.   FreeWorkbench();
  153.  }
  154.  /* call failed */
  155.  return(NULL);
  156. }
  157.  
  158. /* Delete an Icon object */
  159. BOOL DeleteTMObjectIcon(struct TMObjectIcon *tmobj)
  160. {
  161.  DEBUG_PRINTF("Delete/Icon (0x%08lx)\n",tmobj);
  162.  
  163.  /* Free resources */
  164.  if (tmobj->io_AppIcon) {
  165.   SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  166.   FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  167.  }
  168.  FreeWorkbench();
  169.  
  170.  /* Remove links */
  171.  if (tmobj->io_ExecLink) RemLinkTMObject(tmobj->io_ExecLink);
  172.  if (tmobj->io_ImageLink)
  173.   RemLinkTMObject((struct TMLink *) tmobj->io_ImageLink);
  174.  if (tmobj->io_SoundLink) RemLinkTMObject(tmobj->io_SoundLink);
  175.  
  176.  /* Remove object from list */
  177.  Remove((struct Node *) tmobj);
  178.  
  179.  /* Free object */
  180.  FreeMem(tmobj,sizeof(struct TMObjectIcon));
  181.  
  182.  /* All OK. */
  183.  return(TRUE);
  184. }
  185.  
  186. /* Change an Icon object */
  187. struct TMObject *ChangeTMObjectIcon(struct TMHandle *handle,
  188.                                     struct TMObjectIcon *tmobj,
  189.                                     struct TagItem *tags)
  190. {
  191.  struct TagItem *ti,*tstate;
  192.  struct TMLinkImage *oldimage=NULL;
  193.  void *oldicon=NULL;
  194.  
  195.  /* Scan tag list */
  196.  tstate=tags;
  197.  while (ti=NextTagItem(&tstate)) {
  198.  
  199.   DEBUG_PRINTF("Got Tag (0x%08lx)\n",ti->ti_Tag);
  200.  
  201.   switch (ti->ti_Tag) {
  202.    case TMOP_Exec:     if (tmobj->io_ExecLink) { /* Already got a link? */
  203.                         /* Yes, remove it! */
  204.                         RemLinkTMObject(tmobj->io_ExecLink);
  205.                         tmobj->io_ExecLink=NULL;
  206.                        }
  207.  
  208.                        if (ti->ti_Data) {
  209.                         /* Create new link to exec object */
  210.                         tmobj->io_ExecLink=AddLinkTMObject(handle,
  211.                                                            (char *) ti->ti_Data,
  212.                                                            TMOBJTYPE_EXEC,
  213.                                                            (struct TMObject *)
  214.                                                             tmobj);
  215.                        }
  216.                        break;
  217.    case TMOP_Image:    oldimage=tmobj->io_ImageLink;
  218.                        oldicon=tmobj->io_AppIcon;
  219.  
  220.                        if (ti->ti_Data) {
  221.                         /* Create new link to exec object */
  222.                         tmobj->io_ImageLink=(struct TMLinkImage *)
  223.                                   AddLinkTMObject(handle, (char *) ti->ti_Data,
  224.                                                   TMOBJTYPE_IMAGE,
  225.                                                   (struct TMObject *) tmobj);
  226.                        }
  227.                        break;
  228.    case TMOP_LeftEdge: tmobj->io_LeftEdge=ti->ti_Data;
  229.                        oldicon=tmobj->io_AppIcon;
  230.                        break;
  231.    case TMOP_Sound:    if (tmobj->io_SoundLink) { /* Already got a link? */
  232.                         /* Yes, remove it! */
  233.                         RemLinkTMObject(tmobj->io_SoundLink);
  234.                         tmobj->io_SoundLink=NULL;
  235.                        }
  236.  
  237.                        if (ti->ti_Data) {
  238.                         /* Create new link to exec object */
  239.                         tmobj->io_SoundLink=AddLinkTMObject(handle,
  240.                                                             (char *)
  241.                                                              ti->ti_Data,
  242.                                                             TMOBJTYPE_SOUND,
  243.                                                             (struct TMObject *)
  244.                                                              tmobj);
  245.                        }
  246.                        break;
  247.    case TMOP_TopEdge:  tmobj->io_TopEdge=ti->ti_Data;
  248.                        oldicon=tmobj->io_AppIcon;
  249.                        break;
  250.   }
  251.  }
  252.  
  253.  /* Set new flags */
  254.  {
  255.   ULONG oldflags=tmobj->io_Flags;
  256.  
  257.   /* Flags changed? */
  258.   if ((tmobj->io_Flags=PackBoolTags(oldflags,tags,flagmap)) != oldflags)
  259.    /* Yes. Create new icon */
  260.    oldicon=tmobj->io_AppIcon;
  261.  }
  262.  
  263.  /* Create new icon? */
  264.  if (oldicon) {
  265.   /* Yes! Remove old one */
  266.   SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  267.   tmobj->io_AppIcon=NULL;
  268.   FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  269.   tmobj->io_DiskObj=NULL;
  270.  
  271.   /* Remove old image link? */
  272.   if (oldimage) RemLinkTMObject((struct TMLink *) oldimage);
  273.  
  274.   /* Create AppIcon */
  275.   CreateAppIcon(tmobj,tmobj->io_Object.tmo_Name);
  276.  }
  277.  
  278.  /* All OK */
  279.  return(TRUE);
  280. }
  281.  
  282. /* Update link structures */
  283. void DeleteLinkTMObjectIcon(struct TMLink *tml)
  284. {
  285.  struct TMObjectIcon *tmobj=(struct TMObjectIcon *) tml->tml_LinkedTo;
  286.  
  287.  /* Clear link */
  288.  if (tml==tmobj->io_ExecLink)
  289.   tmobj->io_ExecLink=NULL;
  290.  else if (tml==(struct TMLink *) tmobj->io_ImageLink) {
  291.   if (tmobj->io_AppIcon) {
  292.    SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  293.    tmobj->io_AppIcon=NULL;
  294.   }
  295.   if (tmobj->io_DiskObj) {
  296.    FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  297.    tmobj->io_DiskObj=NULL;
  298.   }
  299.   tmobj->io_ImageLink=NULL;
  300.  }
  301.  else if (tml==tmobj->io_SoundLink)
  302.   tmobj->io_SoundLink=NULL;
  303. }
  304.  
  305. /* Activate an Icon object */
  306. void ActivateTMObjectIcon(struct TMLink *tml, struct AppMessage *msg)
  307. {
  308.  struct TMObjectIcon *tmobj=(struct TMObjectIcon *) tml->tml_Linked;
  309.  
  310.  /* Activate Sound object */
  311.  if (tmobj->io_SoundLink) CallActivateTMObject(tmobj->io_SoundLink,NULL);
  312.  
  313.  /* Activate Exec object */
  314.  if (tmobj->io_ExecLink) CallActivateTMObject(tmobj->io_ExecLink,msg);
  315. }
  316.  
  317.